/**
 * 版本号排序
 * ['0.1.1', '2.3.3', '0.302.1', '4.2', '4.3.5', '4.3.4.5']。
 * 现在需要对其进行排序，
 * 排序的结果为 ['4.3.5','4.3.4.5','2.3.3','0.302.1','0.1.1']
 * ? 利用sort方法， 自定义排序规则
 */

function compareVersion(v1, v2) {
  const arr1 = v1.split('.').map(Number)
  const arr2 = v2.split('.').map(Number)
  const len = Math.max(arr1.length, arr2.length);
  for (let i = 0; i < len; i++) {
    if (arr1[i] == arr2[i])  continue;
    return arr1[i]-arr2[i] // 升序 
}
}
// arr = ['0.1.1', '2.3.3', '0.302.1', '4.2', '4.3.5', '4.3.4.5']
arr = ['1.45.0', '1.45', '6', '3.3.3.3.3.3.3', ]
console.log(arr.sort((a, b) => compareVersion(a, b)))

/**
只比较版本号
 */
var compareVersion = function(version1, version2) {
  const n1 = version1.split('.').map(Number)
  const n2 = version2.split('.').map(Number)
  for(let i=0;i< Math.max(n1.length, n2.length);i++) {
    const x = n1[i] || 0
    const y = n2[i] || 0
    if(x==y) continue
    return x>y ? 1 :-1
  }
  return 0
};

console.log(compareVersion('1.45.0', '1.45'))